import re
import urllib


def getImgList(url):

    reg = r'src="(.+?\.jpg)" pic_ext'
    imgre = re.compile(reg)
    return re.findall(reg,urllib.urlopen(url).read())

def saveImg( imgurl,fileSavePath):
    cont = urllib.urlopen(imgurl).read()
    filename =  re.search(r"(\w+).jpg",imgurl).group()
    fileFullPathName = fileSavePath + filename
    f = open(fileFullPathName,'w+')
    print 'downloading file :' + filename + '\nPath:'+fileSavePath
    f.write(cont)
    f.close()      


srcurl = "http://tieba.baidu.com/p/2460150866"
fileLocation = "Data/"

for imageurl in getImgList(srcurl):
    saveImg(imageurl,fileLocation)
print 'down'
